

package com.io.controller;

import com.io.constant.Constant;
import com.io.dto.ApiResult;
import com.io.entity.SysMenuEntity;
import com.io.service.SysMenuService;
import com.io.utils.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统菜单
 *
 * @author guodd
 */
@Api(tags = "系统菜单")
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController extends BaseController {
    @Autowired
    private SysMenuService sysMenuService;

    @ApiOperation("导航菜单")
    @GetMapping("/nav")
    public ApiResult nav() {
        List<SysMenuEntity> menuList = sysMenuService.getUserMenuList(getUserId());
        return ApiResult.ok(menuList);
    }

    @ApiOperation("所有菜单列表")
    @RequestMapping("/list")
    @RequiresPermissions("sys:menu:list")
    public ApiResult list() {
        List<SysMenuEntity> menuList = sysMenuService.list();
        for (SysMenuEntity sysMenuEntity : menuList) {
            SysMenuEntity parentMenuEntity = sysMenuService.getById(sysMenuEntity.getParentId());
            if (parentMenuEntity != null) {
                sysMenuEntity.setParentName(parentMenuEntity.getName());
            }
        }
        return ApiResult.ok(menuList);
    }

    @ApiOperation("规范-选择菜单(添加、修改菜单)")
    @RequestMapping("/select")
    @RequiresPermissions("sys:menu:select")
    public ApiResult select() {
        //查询列表数据
        List<SysMenuEntity> menuList = sysMenuService.queryNotButtonList();
        // 添加顶级菜单
        SysMenuEntity root = new SysMenuEntity();
        root.setId(0L);
        root.setName("一级菜单");
        root.setParentId(0L);
        root.setOpen(true);
        root.setChildren(menuList);
        return ApiResult.ok(root);
    }

    @ApiOperation("菜单信息")
    @GetMapping("/info/{menuId}")
    @RequiresPermissions("sys:menu:info")
    public ApiResult info(@PathVariable("menuId") Long menuId) {
        SysMenuEntity menu = sysMenuService.getById(menuId);
        return ApiResult.ok(menu);
    }

    @ApiOperation("保存")
    @SysLog("保存菜单")
    @RequestMapping("/save")
    @RequiresPermissions("sys:menu:save")
    public ApiResult save(@RequestBody SysMenuEntity menu) {
        //数据校验
        verifyForm(menu);

        sysMenuService.save(menu);

        return ApiResult.ok();
    }

    @ApiOperation("修改")
    @SysLog("修改菜单")
    @RequestMapping("/update")
    @RequiresPermissions("sys:menu:update")
    public ApiResult update(@RequestBody SysMenuEntity menu) {
        //数据校验
        verifyForm(menu);
        sysMenuService.updateById(menu);
        return ApiResult.ok();
    }

    @ApiOperation("删除")
    @SysLog("删除菜单")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:menu:delete")
    public ApiResult delete(long menuId) {
        if (menuId <= 40) {
            return ApiResult.error("系统菜单，不能删除");
        }

        //判断是否有子菜单或按钮
        List<SysMenuEntity> menuList = sysMenuService.queryListParentId(menuId);
        if (menuList.size() > 0) {
            return ApiResult.error("请先删除子菜单或按钮");
        }

        sysMenuService.delete(menuId);

        return ApiResult.ok();
    }

    /**
     * 验证参数是否正确
     */
    private void verifyForm(SysMenuEntity menu) {
        if (StringUtils.isBlank(menu.getName())) {
            throw new SecurityException("菜单名称不能为空");
        }

        if (menu.getParentId() == null) {
            throw new SecurityException("上级菜单不能为空");
        }

        // 菜单
        if (menu.getType() == Constant.MenuType.MENU.getValue()) {
            if (StringUtils.isBlank(menu.getUrl())) {
                throw new SecurityException("菜单URL不能为空");
            }
        }

        // 上级菜单类型
        int parentType = Constant.MenuType.CATALOG.getValue();
        if (menu.getParentId() != 0) {
            SysMenuEntity parentMenu = sysMenuService.getById(menu.getParentId());
            parentType = parentMenu.getType();
        }

        // 目录、菜单
        if (menu.getType() == Constant.MenuType.CATALOG.getValue() ||
            menu.getType() == Constant.MenuType.MENU.getValue()) {
            if (parentType != Constant.MenuType.CATALOG.getValue()) {
                throw new SecurityException("上级菜单只能为目录类型");
            }
            return;
        }

        // 按钮
        if (menu.getType() == Constant.MenuType.BUTTON.getValue()) {
            if (parentType != Constant.MenuType.MENU.getValue()) {
                throw new SecurityException("上级菜单只能为菜单类型");
            }
        }
    }
}
